home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / emacs.lha / emacs-19.16 / lisp / vc-hooks.el < prev    next >
Lisp/Scheme  |  1993-07-06  |  10KB  |  275 lines

  1. ;;; vc-hooks.el --- resident support for version-control
  2.  
  3. ;; Copyright (C) 1992 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
  6. ;; Version: 5.3
  7.  
  8. ;; This file is part of GNU Emacs.
  9.  
  10. ;; GNU Emacs is free software; you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 2, or (at your option)
  13. ;; any later version.
  14.  
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. ;; GNU General Public License for more details.
  19.  
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  22. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  
  24. ;;; Commentary:
  25.  
  26. ;; See the commentary of vc.el.
  27.  
  28. ;;; Code:
  29.  
  30. (defvar vc-master-templates
  31.   '(("%sRCS/%s,v" . RCS) ("%s%s,v" . RCS) ("%sRCS/%s" . RCS)
  32.     ("%sSCCS/s.%s" . SCCS) ("%ss.%s". SCCS))
  33.   "*Where to look for version-control master files.
  34. The first pair corresponding to a given back end is used as a template
  35. when creating new masters.")
  36.  
  37. (defvar vc-make-backup-files nil
  38.   "*If non-nil, backups of registered files are made according to
  39. the make-backup-files variable.  Otherwise, prevents backups being made.")
  40.  
  41. (defvar vc-rcs-status t
  42.   "*If non-nil, revision and locks on RCS working file displayed in modeline.
  43. Otherwise, not displayed.")
  44.  
  45. ;; Tell Emacs about this new kind of minor mode
  46. (if (not (assoc 'vc-mode minor-mode-alist))
  47.     (setq minor-mode-alist (cons '(vc-mode vc-mode)
  48.                  minor-mode-alist)))
  49.  
  50. (make-variable-buffer-local 'vc-mode)
  51. (put 'vc-mode 'permanent-local t)
  52.  
  53. ;; We need a notion of per-file properties because the version
  54. ;; control state of a file is expensive to derive --- we don't
  55. ;; want to recompute it even on every find.
  56.  
  57. (defmacro vc-error-occurred (&rest body)
  58.   (list 'condition-case nil (cons 'progn (append body '(nil))) '(error t)))
  59.  
  60. (defvar vc-file-prop-obarray [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
  61.   "Obarray for per-file properties.")
  62.  
  63. (defun vc-file-setprop (file property value)
  64.   ;; set per-file property
  65.   (put (intern file vc-file-prop-obarray) property value))
  66.  
  67. (defun vc-file-getprop (file property)
  68.   ;; get per-file property
  69.   (get (intern file vc-file-prop-obarray) property))
  70.  
  71. ;;; actual version-control code starts here
  72.  
  73. (defun vc-registered (file)
  74.   (let (handler handlers)
  75.     (if (boundp 'file-name-handler-alist)
  76.     (save-match-data
  77.       (setq handlers file-name-handler-alist)
  78.       (while (and (consp handlers) (null handler))
  79.         (if (and (consp (car handlers))
  80.              (stringp (car (car handlers)))
  81.              (string-match (car (car handlers)) file))
  82.         (setq handler (cdr (car handlers))))
  83.         (setq handlers (cdr handlers)))))
  84.     (if handler
  85.     (funcall handler 'vc-registered file)
  86.       ;; Search for a master corresponding to the given file
  87.       (let ((dirname (or (file-name-directory file) ""))
  88.         (basename (file-name-nondirectory file)))
  89.     (catch 'found
  90.       (mapcar
  91.        (function (lambda (s)
  92.           (let ((trial (format (car s) dirname basename)))
  93.         (if (and (file-exists-p trial)
  94.              ;; Make sure the file we found with name
  95.              ;; TRIAL is not the source file itself.
  96.              ;; That can happen with RCS-style names
  97.              ;; if the file name is truncated
  98.              ;; (e.g. to 14 chars).  See if either
  99.              ;; directory or attributes differ.
  100.              (or (not (string= dirname
  101.                        (file-name-directory trial)))
  102.                  (not (equal
  103.                    (file-attributes file)
  104.                    (file-attributes trial)))))
  105.             (throw 'found (cons trial (cdr s)))))))
  106.        vc-master-templates)
  107.       nil)))))
  108.  
  109. (defun vc-name (file)
  110.   "Return the master name of a file, nil if it is not registered."
  111.   (or (vc-file-getprop file 'vc-name)
  112.       (let ((name-and-type (vc-registered file)))
  113.     (if name-and-type
  114.         (progn
  115.           (vc-file-setprop file 'vc-backend (cdr name-and-type))
  116.           (vc-file-setprop file 'vc-name (car name-and-type)))))))
  117.  
  118. (defun vc-backend-deduce (file)
  119.   "Return the version-control type of a file, nil if it is not registered."
  120.   (and file
  121.        (or (vc-file-getprop file 'vc-backend)
  122.        (let ((name-and-type (vc-registered file)))
  123.          (if name-and-type
  124.          (progn
  125.            (vc-file-setprop file 'vc-name (car name-and-type))
  126.            (vc-file-setprop file 'vc-backend (cdr name-and-type))))))))
  127.  
  128. (defun vc-toggle-read-only ()
  129.   "Change read-only status of current buffer, perhaps via version control.
  130. If the buffer is visiting a file registered with version control,
  131. then check the file in or out.  Otherwise, just change the read-only flag
  132. of the buffer."
  133.   (interactive)
  134.   (if (vc-backend-deduce (buffer-file-name))
  135.       (vc-next-action nil)
  136.     (toggle-read-only)))
  137. (define-key global-map "\C-x\C-q" 'vc-toggle-read-only)
  138.  
  139. (defun vc-mode-line (file &optional label)
  140.   "Set `vc-mode' to display type of version control for FILE.
  141. The value is set in the current buffer, which should be the buffer
  142. visiting FILE."
  143.   (interactive (list buffer-file-name nil))
  144.   (let ((vc-type (vc-backend-deduce file)))
  145.     (if vc-type
  146.         (setq vc-mode
  147.               (concat " " (or label (symbol-name vc-type))
  148.               (if (and vc-rcs-status (eq vc-type 'RCS))
  149.                           (vc-rcs-status file)))))
  150.     ;; force update of mode line
  151.     (set-buffer-modified-p (buffer-modified-p))
  152.     vc-type))
  153.  
  154. (defun vc-rcs-status (file)
  155.   ;; Return string " [LOCKER:REV]" if FILE under RCS control, otherwise nil,
  156.   ;; for placement in modeline by `vc-mode-line'.
  157.  
  158.   ;; If FILE is not locked then return just "".  If the FILE is locked
  159.   ;; then return *all* the locks currently set, in a single string of the
  160.   ;; form " LOCKER1:REV1 LOCKER2:REV2 ...".
  161.  
  162.   ;; Algorithm: 
  163.  
  164.   ;; 1. Check for master file corresponding to FILE being visited.
  165.   ;; 
  166.   ;; 2. Insert the first few characters of the master file into a work
  167.   ;; buffer.
  168.   ;;  
  169.   ;; 3. Search work buffer for line starting with "date" indicating enough
  170.   ;; of header was included; if not found, then keep inserting characters
  171.   ;; until "date" is located.
  172.   ;; 
  173.   ;; 4. Search work buffer for line starting with "locks", extract
  174.   ;; all the locks currently enabled, and remove control characters
  175.   ;; separating them, like newlines; the string " user1:revision1
  176.   ;; user2:revision2 ..." is returned.
  177.  
  178.   ;; Limitations:
  179.  
  180.   ;; The output doesn't show which version you are actually looking at.
  181.   ;; The modeline can get quite cluttered when there are multiple locks.
  182.  
  183.   (let ((master (vc-name file))
  184.     found status)
  185.  
  186.     ;; If master file exists, then parse its contents, otherwise we return the 
  187.     ;; nil value of this if form.
  188.     (if master
  189.         (save-excursion
  190.  
  191.           ;; Create work buffer.
  192.           (set-buffer (get-buffer-create "*vc-rcs-status*"))
  193.           (setq buffer-read-only nil
  194.                 default-directory (file-name-directory master))
  195.           (erase-buffer)
  196.  
  197.           ;; Check if we have enough of the header.
  198.       ;; If not, then keep including more.
  199.           (while
  200.           (not (or found
  201.                (let ((s (buffer-size)))
  202.              (goto-char (1+ s))
  203.              (zerop (car (cdr (insert-file-contents
  204.                        master nil s (+ s 8192))))))))
  205.         (beginning-of-line)
  206.         (setq found (re-search-forward "^locks\\([^;]*\\);" nil t)))
  207.  
  208.           (if found
  209.           ;; Clean control characters from text.
  210.           (let ((status
  211.              (save-restriction
  212.                (narrow-to-region (match-beginning 1) (match-end 1))
  213.                (goto-char (point-min))
  214.                (while (re-search-forward "[ \b\t\n\v\f\r]+" nil t)
  215.              (replace-match " " t t))
  216.                (buffer-string))))
  217.         ;; Clean work buffer.
  218.         (erase-buffer)
  219.         (set-buffer-modified-p nil)
  220.         status))))))
  221.  
  222. ;;; install a call to the above as a find-file hook
  223. (defun vc-find-file-hook ()
  224.   ;; Recompute whether file is version controlled,
  225.   ;; if user has killed the buffer and revisited.
  226.   (if buffer-file-name
  227.       (vc-file-setprop buffer-file-name 'vc-backend nil))
  228.   (if (and (vc-mode-line buffer-file-name) (not vc-make-backup-files))
  229.       (progn
  230.     (make-local-variable 'make-backup-files)
  231.     (setq make-backup-files nil))))
  232.  
  233. (or (memq 'vc-find-file-hook find-file-hooks)
  234.     (setq find-file-hooks
  235.       (cons 'vc-find-file-hook find-file-hooks)))
  236.  
  237. ;;; more hooks, this time for file-not-found
  238. (defun vc-file-not-found-hook ()
  239.   "When file is not found, try to check it out from RCS or SCCS.
  240. Returns t if checkout was successful, nil otherwise."
  241.   (if (vc-backend-deduce buffer-file-name)
  242.       (progn
  243.     (require 'vc)
  244.     (not (vc-error-occurred (vc-checkout buffer-file-name))))))
  245.  
  246. (or (memq 'vc-file-not-found-hook find-file-not-found-hooks)
  247.     (setq find-file-not-found-hooks
  248.       (cons 'vc-file-not-found-hook find-file-not-found-hooks)))
  249.  
  250. ;;; Now arrange for bindings and autoloading of the main package.
  251. ;;; Bindings for this have to go in the global map, as we'll often
  252. ;;; want to call them from random buffers.
  253.  
  254. (setq vc-prefix-map (lookup-key global-map "\C-xv"))
  255. (if (not (keymapp vc-prefix-map))
  256.     (progn
  257.       (setq vc-prefix-map (make-sparse-keymap))
  258.       (define-key global-map "\C-xv" vc-prefix-map)
  259.       (define-key vc-prefix-map "a" 'vc-update-change-log)
  260.       (define-key vc-prefix-map "c" 'vc-cancel-version)
  261.       (define-key vc-prefix-map "d" 'vc-directory)
  262.       (define-key vc-prefix-map "h" 'vc-insert-headers)
  263.       (define-key vc-prefix-map "i" 'vc-register)
  264.       (define-key vc-prefix-map "l" 'vc-print-log)
  265.       (define-key vc-prefix-map "r" 'vc-retrieve-snapshot)
  266.       (define-key vc-prefix-map "s" 'vc-create-snapshot)
  267.       (define-key vc-prefix-map "u" 'vc-revert-buffer)
  268.       (define-key vc-prefix-map "v" 'vc-next-action)
  269.       (define-key vc-prefix-map "=" 'vc-diff)
  270.       ))
  271.  
  272. (provide 'vc-hooks)
  273.  
  274. ;;; vc-hooks.el ends here
  275.